package com.loongxun.playwithyou.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.activity.LoginActivity;
import com.loongxun.playwithyou.ui.activity.playuser.MemberOrderActivity;
import com.loongxun.playwithyou.ui.activity.sparring.SparringActivity;
import com.loongxun.playwithyou.ui.activity.user.SetUpActivity;
import com.loongxun.playwithyou.ui.activity.user.UserCollAcitvity;
import com.loongxun.playwithyou.ui.activity.user.UserOrderActivity;
import com.loongxun.playwithyou.util.ToastView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/7/18.
 */
public class MenuLeftFragment extends Fragment implements View.OnClickListener{

    private View view;
    private Context context;
    //用户编辑器
    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    private SendRequest send;
    private String  user_id,user_photo,user_name, member_id = "";

    private ImageLoader imageLoader = ImageLoader.getInstance();

    //头像
    private ImageView iv_g_user_img;
    //名称，陪练人员
    private TextView tv_g_user_name,tv_g_user_member;
    //订单管理，收藏，设置
    private LinearLayout ll_g_user_order,ll_g_user_coll,ll_g_user_setup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        view =  inflater.inflate(R.layout.activity_user, container, false);
        share = context.getSharedPreferences("userInfo", 0);
        editor = share.edit();

        user_id = share.getString("user_id", "");
        if (!user_id.equals("")) {
            initData();
        }

        initView();
        initPhoto();
        return view;
    }

    private void initPhoto(){
        if (!user_id.equals("")) {
            user_photo = share.getString("user_photo","");
            user_name = share.getString("user_name","");
            if (!user_photo.equals("")) {
                if (user_photo.startsWith("http://wx.qlogo.cn/mmopen")) {
                    imageLoader.displayImage(user_photo, iv_g_user_img, App.options);
                }else {
                    imageLoader.displayImage(ApiInterface.IMAGES + user_photo, iv_g_user_img, App.options);
                }
            }
            tv_g_user_name.setText(user_name);
        }
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.userId = user_id;
            //判断此会员是否为陪玩人员
            nHttpClient.getAll(ApiInterface.IS_MEMBER, send.toisMemberJson().toString(), isMemberHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private void initView() {
        iv_g_user_img = (ImageView) view.findViewById(R.id.iv_g_user_img);
        iv_g_user_img.setOnClickListener(this);
        tv_g_user_name = (TextView) view.findViewById(R.id.tv_g_user_name);
        tv_g_user_name.setOnClickListener(this);
        tv_g_user_member = (TextView) view.findViewById(R.id.tv_g_user_member);

        ll_g_user_order = (LinearLayout) view.findViewById(R.id.ll_g_user_order);
        ll_g_user_coll = (LinearLayout) view.findViewById(R.id.ll_g_user_coll);
        ll_g_user_setup = (LinearLayout) view.findViewById(R.id.ll_g_user_setup);
        ll_g_user_order.setOnClickListener(this);
        ll_g_user_coll.setOnClickListener(this);
        ll_g_user_setup.setOnClickListener(this);
        ll_g_user_order.setSelected(true);

        view.findViewById(R.id.ll_g_user_member).setOnClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (data != null){
                user_id = share.getString("user_id", "");
                initData();
                initPhoto();
            }
        }else if (requestCode == 10) {
            if (data != null){
                tv_g_user_name.setText("请登录");
                imageLoader.displayImage("", iv_g_user_img, App.options);
            }
        }
    }

    @Override
    public void onResume() {
        user_id = share.getString("user_id", "");
        if (!"".equals(user_id)) {
            initData();
            initPhoto();
        }
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        user_id = share.getString("user_id", "");
        switch (view.getId()){
            //头像
            case R.id.iv_g_user_img:
            case R.id.tv_g_user_name:
                if (isLogin(user_id)){

                }
                break;
            //订单管理
            case R.id.ll_g_user_order:
                ll_g_user_order.setSelected(true);
                ll_g_user_coll.setSelected(false);
                ll_g_user_setup.setSelected(false);
                if (isLogin(user_id)){
                    intent = new Intent(context, UserOrderActivity.class);
                    startActivity(intent);
                }
                break;
            //我的收藏
            case R.id.ll_g_user_coll:
                ll_g_user_order.setSelected(false);
                ll_g_user_coll.setSelected(true);
                ll_g_user_setup.setSelected(false);
                if (isLogin(user_id)){
                    intent = new Intent(context, UserCollAcitvity.class);
                    startActivity(intent);
                }
                break;
            //设置
            case R.id.ll_g_user_setup:
                ll_g_user_order.setSelected(false);
                ll_g_user_coll.setSelected(false);
                ll_g_user_setup.setSelected(true);
                intent = new Intent(context, SetUpActivity.class);
                startActivityForResult(intent,10);
                break;
            //成为陪练
            case R.id.ll_g_user_member:
                member_id = share.getString("member_id", "");
               if (isLogin(user_id)){
                   if (!"".equals(member_id)) {
                       intent = new Intent(context, MemberOrderActivity.class);
                       startActivity(intent);
                   }else {

                   }
               }
//                intent = new Intent(context, SparringActivity.class);
//                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private boolean isLogin(String user_id) {
        if (user_id.equals("")){
            Intent intent = new Intent(context, LoginActivity.class);
            startActivityForResult(intent,0);
            return false;
        }
        return true;
    }

    private MyHttpClientRespenseHandler isMemberHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                try {
                    String sss = json.getJSONObject("data").getString("playMember");
                    if (!sss.equals("null")){
                        JSONObject jsonObject = json.getJSONObject("data").getJSONObject("playMember");
                        member_id =  jsonObject.getString("member_id");
                        editor.putString("member_id", member_id);
                        editor.commit();//提交修改
                        tv_g_user_member.setText("陪练人员个人中心");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };

}
